package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper flavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            flavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    public PageResult page(DishPageQueryDTO dishDTO) {
        PageHelper.startPage(dishDTO.getPage(),dishDTO.getPageSize());
        Page<DishVO> page = dishMapper.query(dishDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //起售中的菜品不能删除
        List<Dish> dishList = dishMapper.findByIds(ids);
        List<Integer> collect = dishList.stream().map(Dish::getStatus).collect(Collectors.toList());
        if (collect.contains(1)){//TODO 抛异常而不是返回
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //当前菜品关联了套餐,不能删除
        List<Long> setmealids = setmealDishMapper.findByiIds(ids);
        if (!CollectionUtils.isEmpty(setmealids)){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品数据
        dishMapper.detele(ids);
        //删除口味
        flavorMapper.detele(ids);
    }

    /**
     * 菜品详情
     *
     * @param id
     */
    @Override
    public DishVO pagebyId(Long id) {
        DishVO page = dishMapper.queryById(id);
        List<DishFlavor> flavors = flavorMapper.findById(id);
        page.setFlavors(flavors);
        return page;
    }

    @Override
    public void update(DishDTO dishDTO) {
        //删除口味
        ArrayList<Long> longs = new ArrayList<>();
        longs.add(dishDTO.getId());
        flavorMapper.detele(longs);
        //更新菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //添加口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
            flavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Dish category = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(category);
    }
}
